package com.paishen.peiniwan.module.date;

import android.os.Bundle;

import com.paishen.peiniwan.BaseActivity;
import com.paishen.peiniwan.R;
import com.paishen.peiniwan.frame.widget.PagerLayout;
import com.paishen.common.widget.TitleLayout;
import com.paishen.peiniwan.module.profile.MainProfileCouponLayout;
import com.paishen.common.util.BaseUtil;

public class DatePayActivity extends BaseActivity {


    public static final String EXTRA_DATE_PAY_OID = "extra_date_pay_oid";
    public static final String EXTRA_DATE_PAY_PRICE = "extra_date_pay_price";
    public static final String EXTRA_DATE_PAY_BOOKID = "extra_date_pay_bookid";

    private long oid, bookid;
    private float price;

    private TitleLayout layTitle;
    private PagerLayout layPager;

    private DatePayInfoLayout layDatePayInfo;
    private MainProfileCouponLayout layMainProfileCoupon;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        oid = getIntent().getLongExtra(EXTRA_DATE_PAY_OID, -1);
        price = getIntent().getFloatExtra(EXTRA_DATE_PAY_PRICE, -1);
        bookid = getIntent().getLongExtra(EXTRA_DATE_PAY_BOOKID, -1);

        if (oid < 0 || price < 0 ||bookid < 0) {
            BaseUtil.showToast("DatePayActivity : empty or error params");
            finish();
            return;
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_pay);

        layTitle = (TitleLayout) findViewById(R.id.lay_title);
        layTitle.pushContent(makeBackView(), getString(R.string.date_pay), null, false);

        layDatePayInfo = new DatePayInfoLayout(this);
        layDatePayInfo.setData(oid, price, bookid);
        layDatePayInfo.setOnCouponClickListener(new DatePayInfoLayout.OnCouponClickListener() {
            @Override
            public void onCouponClick() {
                layMainProfileCoupon = new MainProfileCouponLayout(DatePayActivity.this);
                layMainProfileCoupon.setValidMode(true);
                layTitle.pushNewContent(makeBackView(), getString(R.string.date_pay_use_coupon), null, true);
                layPager.pushNewContent(layMainProfileCoupon);


                layMainProfileCoupon.setOnItemClickListener(new MainProfileCouponLayout.OnItemClickListener() {
                    @Override
                    public void onCouponClick(com.paishen.peiniwan.database.Coupon coupon) {
                        if (!coupon.getIs_valid()) {
                            BaseUtil.showToast(R.string.date_pay_use_coupon_error_valid_tips);
                            return;
                        }

                        layDatePayInfo.setDataByNewCoupon(coupon);
                        onBackPressed();
                    }
                });
            }
        });

        layPager = (PagerLayout) findViewById(R.id.lay_pager);
        layPager.pushNewContent(layDatePayInfo, false);

        layPager.setOnStateChangedListener(new PagerLayout.OnStateChangedListener() {
            @Override
            public void upToCurrent() {
                layTitle.returnToCurrentContent();
            }

            @Override
            public void backToLast() {
                layTitle.popToLastContent();
            }

            @Override
            public void touchMove(float progress) {
                layTitle.setToLastAnimationProgress(progress);
            }
        });

    }

    @Override
    public void onBackPressed() {
        if (!layPager.isOnlyOnePageOrEmpty()) {
            layTitle.popToLastContent();
            layPager.popToLastContent();
        } else {
            super.onBackPressed();
        }
    }
}
